package com.springbootcrud.user.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.springbootcrud.user.entity.Crud;
import com.springbootcrud.user.entity.User;
import com.springbootcrud.user.service.CrudService;
import org.apache.ibatis.annotations.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Created by Sun on 2020/4/10.
 */
@Controller
@RequestMapping("Crud")
public class CrudController {

    @Autowired
    CrudService crudService;;

    @ResponseBody
    @RequestMapping("/getAll")
    public List<CrudController> getAll(Map map){
        return crudService.getAll(map);
    }
    @RequestMapping("/delete")
    public String delete(Integer id){
        crudService.delete(id);
        return "redirect:/Crud.html";
    }
    //新增用户
    @ResponseBody
    @RequestMapping("/save")
    public void save(@RequestBody Crud crud){
        crudService.save(crud);
    }

    //修改,先根据Id查询,实现页面回显功能
    @ResponseBody
    @RequestMapping("/getById")
    public Crud getById(Integer id){
        return crudService.getById(id);
    }

    @RequestMapping("/savesaveCrud")
    @ResponseBody
    public void saveCrud(@RequestBody Crud crud){
        crudService.saveCrud(crud);
    }

    //分页查所有
    @ResponseBody
    @RequestMapping("/pageAll")
    public List<Crud> pageAll(){
       return crudService.queryUserInfo();
    }

    //使用分页插件
    @GetMapping("/testPageHelper1")
    public PageInfo<Crud> testPageHelper1(){
        PageInfo<Crud>  queryResult = crudService.findAllUserByPageS(1,5);
        System.out.println("分页插件使用第一步");
        System.out.println(queryResult);
        return queryResult;
    }
    @GetMapping("/testPageHelper2")
    public List<Crud> testPageHelper2(){
        List<Crud> queryResult = crudService.findAllUserByPageF(1,5);
        System.out.println("分页插件使用第二步");
        System.out.println(queryResult);
        return queryResult;
    }



}
